<template>
  <div class="personal" ref="personal">
    <div class="personal-wrapper">
      <v-header></v-header>
      <div class="personal-body">
        <personal-title class="account-title" title="我的活動獎勵"></personal-title>
        <div class="cash-container">
          <div class="cash">
            <span class="cash-number">{{initChexCount + invitedCount * 6}}</span>
            <span class="cash-unit">CHEX</span>
          </div>
          <el-button @click="drawCash" class="cash-btn">提現</el-button>
          <span class="uid-msg" v-show="uid">{{'UID:' + uid}}</span>
          <span class="diamond"></span>
        </div>
        <div class="personal-module invite-module">
          <personal-title title="邀請好友"></personal-title>
          <p class="invite-description">活動期間每邀請一位好友，即獎勵6CHEX幣
              活動結束後統一發放到CHAOEX錢包</p>
          <el-button type="primary" @click="iniviteFriend" class="submit-btn">立即邀請好友</el-button>
          <div class="invite-detail-container">
            <div class="invite-count-module">
              <div class="invite-count-module-inner">
                <p class="invite-description">成功邀請人數</p>
                <p class="invite-count">{{invitedCount}}</p>
              </div>
            </div>
            <div class="invite-award-module">
              <div class="invite-award-module_inner">
                <p class="invite-description">預計邀請獎勵</p>
                <p class="invite-count">{{initChexCount + invitedCount * 6}}</p>
              </div>
            </div>
          </div>
        </div>
        <div class="personal-module follow-module">
            <personal-title title="關注動態"></personal-title>
            <p class="follow-description">關注CHAOEX公眾號，GET更多活動</p>
            <div class="qrcode-container">
              <img src="../assets/images/wx-qrcode.jpg" alt="" class="qrcode-img">
              <p class="qrcode-description">-長按識別二維碼-</p>
            </div>
          </div>
      </div>
    </div>
  </div>
</template>

<script>
import Header from '@/components/Header'
import PersonalTitle from '@/components/PersonalTitle'
import { getInvitedCount, getRegisterTime } from '@/Api/request'
import { mapGetters } from 'vuex'

// 活动开始时间
const ACTIVITY_TIME = '2018-06-05T14:00:00'
export default {
  name: 'personal',
  data () {
    return {
      invitedCount: 0,
      initChexCount: 0
    }
  },
  computed: {
    ...mapGetters([
      'token',
      'uid'
    ])
  },
  created () {
    this._getRegisterTime()
    this._getInvitedInfo()
  },
  mounted () {
    this.$nextTick(() => {
      this._initScroll()
    })
  },
  methods: {
    _initScroll () {
      this.personalScroll = new this.$BScroll(this.$refs.personal, {
        click: true,
        preventDefaultException: {
          className: /(^|\s)qrcode-img(\s|$)/
        }
      })
    },
    iniviteFriend () {
      this.$router.push({
        name: 'invite'
      })
    },
    _getRegisterTime () {
      if (this.uid) {
        getRegisterTime(this.uid).then(res => {
          if (Date.parse(res.create_time.substring(0, res.create_time.length - 5)) >= Date.parse(ACTIVITY_TIME)) {
            this.initChexCount = 4
          } else {
            this.initChexCount = 0
          }
        })
      } else {
        this.initChexCount = 0
      }
    },
    _getInvitedInfo () {
      if (this.uid) {
        const dataStr = this.uid
        getInvitedCount(dataStr).then(res => {
          this.invitedCount = res
        })
      } else {
        this.$message.error({
          message: '您已退出，請重新登錄',
          center: true
        })
      }
    },
    drawCash () {
      this.$router.push({
        name: 'cash',
        params: {
          chexCount: this.initChexCount + this.invitedCount * 6
        }
      })
    }
  },
  components: {
    vHeader: Header,
    PersonalTitle
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
.personal-wrapper{
  padding: 37px 49px 57px;
}
.header{
  margin-bottom: 64px;
}
.personal-body{
  box-sizing: border-box;
  padding: 44px 32px 38px;
  background: #fff;
  .account-title{
    margin-bottom: 27px;
  }
  .cash-container{
    position: relative;
    display: flex;
    box-sizing: border-box
    width: 100%;
    padding: 58px 40px 66px 22px;
    margin: 0 auto 47px;
    border-radius: 8px;
    background: linear-gradient(to right bottom, #EAAB00, #FFCA00);
    font-size: 0;
    .cash{
      flex: 1;
      height: 66px;
      color: rgba(255,255,255,1);
      .cash-number{
        display: inline-block;
        vertical-align: bottom;
        font-size: 66px;
        line-height: 66px;
      }
      .cash-unit{
        display: inline-block;
        vertical-align: bottom;
        font-size: 22px;
        line-height: 22px;
      }
    }
    .cash-btn{
      flex: 0 0 142px;
      width: 142px;
      height: 51px;
      margin-top: 12px;
      background: rgba(255,255,255,0);
      border-radius: 26px;
      font-size: 28px;
      color: rgba(255,255,255,1);
    }
    .uid-msg{
      position: absolute;
      left: 22px;
      bottom: 20px;
      width: 400px;
      height: 26px;
      font-size: 26px;
      color: #fff;
      line-height: 26px;
      background: rgba(255, 255, 255, 0)
    }
    .diamond{
      position: absolute;
      right: 6px;
      bottom: 0;
      width: 121px;
      height: 80px;
      background: url("../assets/images/diamond.png") no-repeat;
      background-size: cover;
    }
  }
}

.personal-module{
  position: relative;
  padding: 41px 41px 50px;
  &:after {
    display: block;
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    border-top: 1px solid rgba(220,220,220,1); /*no*/
    content: '';
  }
  .personal-title{
    margin-bottom: 32px;
  }
}
@media (-webkit-min-device-pixel-ratio: 1.5),(min-device-pixel-ratio: 1.5) {
  .personal-module{
    &:after {
      -webkit-transform: scaleY(0.7);
      transform: scaleY(0.7);
    }
  }
}
@media (-webkit-min-device-pixel-ratio: 2),(min-device-pixel-ratio: 2) {
  .personal-module{
    &:after{
      -webkit-transform: scaleY(0.5);
      transform: scaleY(0.5);
    }
  }
}

.invite-module{
  .invite-description{
    width: 100%;
    margin-bottom: 30px;
    font-size: 26px;
    color: rgba(38,156,255,1);
    line-height: 36px;
    text-align: center;
  }
  .submit-btn{
    width: 100%;
    height: 91px;
    margin-bottom: 36px;
    border-radius: 46px;
    background: rgba(24,150,255,0.58);
    font-size: 36px;
  }
  .invite-detail-container{
    display: flex;
    width: 100%;
    .invite-count-module{
      flex: 1;
      margin-right: 32px;
    }
    .invite-award-module{
      flex: 1;
    }
    .invite-count-module-inner, .invite-award-module_inner{
      box-sizing: border-box;
      width: 100%;
      height: 155px;
      padding: 28px 0;
      margin: 0 auto;
      background: rgba(244,247,253,1);
      border-radius: 8px;
      .invite-description{
        margin-bottom: 24px
        font-size: 26px;
        color: rgba(38,156,255,1);
        line-height: 26px;
        text-align: center;
      }
      .invite-count{
        font-size: 43px;
        color: rgba(27,151,255,1);
        line-height: 43px;
        text-align: center;
      }
    }
  }
}

.follow-module{
  padding-bottom: 0;
  .follow-description{
    margin-bottom: 27px;
    font-size: 26px;
    color: rgba(102,102,102,1);
    line-height: 26px;
    text-align: center;
  }
  .qrcode-container{
    width:223px;
    margin: 0 auto;
    text-align: center;
    font-size: 0;
    .qrcode-img{
      width: 223px;
      height: 215px;
      margin-bottom: 13px;
    }
    .qrcode-description{
      font-size: 24px;
      color: rgba(102,102,102,1);
      line-height: 24px;
    }
  }
}
</style>
